//
//  NSObject+MagicalModel.m
//  DBManagerDemo
//
//  Created by CRY on 16/6/30.
//  Copyright © 2016年 CRY. All rights reserved.
//

#import "NSObject+Magical.h"
#import "TRAdditions.h"

@implementation NSObject (Magical)

+ (NSArray *)tr_findAll{
    
    return [self tr_findByParameters:nil];
}

+ (NSArray *)tr_findAllOrderedByAttribute:(NSString *)attribute
                                ascending:(BOOL)ascending{
    return [self tr_findByParameters:nil
                  orderedByAttribute:attribute
                           ascending:ascending];
}

+ (NSArray *)tr_findAllOrderedByAttribute:(NSString *)attribute
                                ascending:(BOOL)ascending
                                    limit:(NSInteger)limit
                                   offset:(NSInteger)offset{
    return [self tr_findByParameters:nil
                  orderedByAttribute:attribute
                           ascending:ascending
                               limit:limit
                              offset:offset];
}

+ (NSArray *)tr_findByAttribute:(NSString *)attribute
                          value:(NSString *)value{
    return [self tr_findByParameters:@[[NSString stringWithFormat:@"%@='%@'",attribute,value]]];
}

+ (NSArray *)tr_findByParameters:(NSArray *)params{
    
    return [TRAdditions findAllWithObject:self
                                parameter:params
                       orderedByAttribute:nil
                                ascending:NO
                                    limit:0
                                   offset:0];
}

+ (NSArray *)tr_findByParameters:(NSArray *)params
              orderedByAttribute:(NSString *)attribute
                       ascending:(BOOL)ascending{
    
    return [TRAdditions findAllWithObject:self
                                parameter:params
                       orderedByAttribute:attribute
                                ascending:ascending
                                    limit:0
                                   offset:0];
}

+ (NSArray *)tr_findByParameters:(NSArray *)params
              orderedByAttribute:(NSString *)attribute
                       ascending:(BOOL)ascending
                           limit:(NSInteger)limit
                          offset:(NSInteger)offset{
    
    return [TRAdditions findAllWithObject:self
                                parameter:params
                       orderedByAttribute:attribute
                                ascending:ascending
                                    limit:limit
                                   offset:offset];
}

+ (instancetype)tr_findFirstOrderedByAttribute:(NSString *)attribute
                                     ascending:(BOOL)ascending{
    
    return [[self tr_findByParameters:nil
                   orderedByAttribute:attribute
                            ascending:ascending] firstObject];
}

- (void)tr_saveToPersistentStoreAndWaitWithFinished:(FinishedBlock)finished{
    
    [TRAdditions saveToPersistentStoreAndWaitWithObject:self
                                             primaryKey:[self tr_setPrimaryKey]
                                               finished:finished];
}

- (void)tr_deleteWithFinished:(FinishedBlock)finished{
    
    [TRAdditions deleteWithObject:self finished:finished];
}

- (NSString *)tr_setPrimaryKey{
    return @"";
}

@end
